<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form method="post" class="layui-form" action="/insertadmin">
            <div class="layui-form-item">
                <label for="admin_name_n" class="layui-form-label">
                    <span class="x-red">*</span>登录名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="admin_name_n" name="admin_name_n" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>将会成为您唯一的登入名
                </div>
            </div>
            <div class="layui-form-item">
                <label for="admin_password" class="layui-form-label">
                    <span class="x-red">*</span>密码
                </label>
                <div class="layui-input-inline">
                    <input type="password" id="admin_password" name="admin_password" required="" lay-verify="pass"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    6到16个字符
                </div>
            </div>
            <div class="layui-form-item">
                <label for="password" class="layui-form-label">
                    <span class="x-red">*</span>确认密码
                </label>
                <div class="layui-input-inline">
                    <input type="password" id="password" name="password" required="" lay-verify="pass1"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="admin_remarks" class="layui-form-label">
                    <span class="x-red">*</span>备注
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="admin_remarks" name="admin_remarks" required="" lay-verify=""
                           autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label  class="layui-form-label">
                </label>
                <button  class="layui-btn" lay-filter="add" lay-submit="">
                    增加
                </button>
            </div>
        </form>
    </div>
</div>
<script>layui.use(['form', 'layer'],
    function() {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        //自定义验证规则
        form.verify({
            required: function(value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            },
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            pass1: function(value) {
                if ($('#admin_password').val() != $('#password').val()) {
                    return '两次密码不一致';
                }
            }
        },1000);

        //监听提交
        form.on('submit(add)',
            function(data1) {
                console.log("111"+JSON.stringify(data1.field));
                var admin_name_n = $("#admin_name_n").val();
                var admin_password = $("#admin_password").val();
                var admin_remarks = $("#admin_remarks").val();
                $.ajax({
                    type:"post",
                    url:"/insertAdmin",
                    data : JSON.stringify({
                        admin_name_n:admin_name_n,
                        admin_password:admin_password,
                        admin_remarks:admin_remarks
                    }),
                    contentType : "application/json;charset=UTF-8",
                    success:function(data) {
                        console.log(data);
                        if(data.status==401){
                            alert("熔断开启，系统异常");
                            window.location.href="login.html";
                            return;
                        }
                        if(data.status==200) {
                            layer.alert("添加成功", {icon: 6},function () {
                                var index=parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                                window.parent.location.reload();//刷新父页面
                                parent.layer.close(index);//关闭弹出层
                            });
                            return;
                        }
                        if(data.status==500) {
                            alert("添加错误");
                            window.location.href="adminlist.html";
                            return;
                        }
                        if(data.status==null) {
                            alert("修改异常");
                            window.location.href="adminlist.html";
                            return;
                        }
                        if(data.status==20) {
                            layer.alert("重复添加", {icon: 6},function () {
                                var index=parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                                window.parent.location.reload();//刷新父页面
                                parent.layer.close(index);//关闭弹出层
                            });
                            return;
                        }
                    }
                });
                return false;
            });

    });
</script>
</body>

</html>
